﻿using AutoMapper;
using Domain.Entity;
using Domain.Entity.Fy;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
	public class HFRepository : IHFRepository
	{
		private readonly IMapper mapper;
		private readonly MyDbContext myDb;

		public HFRepository(IMapper mapper,MyDbContext myDb) 
		{
			this.mapper = mapper;
			this.myDb = myDb;
		}

		public async Task<bool> DeleteHF(int id)
		{
			var zhi = await myDb.HF.SingleOrDefaultAsync(e => e.HufId == id);
			if (zhi != null)
			{
				myDb.HF.Remove(zhi);
				await myDb.SaveChangesAsync();
				return true;
			}
			return false;    
		}
		
		//多条件查询 档案状态为复核通过的数据
		public List<HF> GetHF(string HumanFileStatus, string? FirstKindName, string? SecondKindName, string? ThirdKindName, string? HumanMajorKindName, string? HunmaMajorName, DateTime? startDate, DateTime? endDate)
		{

			var query = myDb.HF.AsQueryable();

			if (!string.IsNullOrEmpty(HumanFileStatus))
			{
				query = query.Where(hf => hf.HumanFileStatus == HumanFileStatus);
			}

			if (!string.IsNullOrEmpty(FirstKindName))
			{
				query = query.Where(hf => hf.FirstKindName == FirstKindName);
			}

			if (!string.IsNullOrEmpty(SecondKindName))
			{
				query = query.Where(hf => hf.SecondKindName == SecondKindName);
			}

			if (!string.IsNullOrEmpty(ThirdKindName))
			{
				query = query.Where(hf => hf.ThirdKindName == ThirdKindName);
			}

			if (!string.IsNullOrEmpty(HumanMajorKindName))
			{
				query = query.Where(hf => hf.HumanMajorKindName == HumanMajorKindName);
			}

			if (!string.IsNullOrEmpty(HunmaMajorName))
			{
				query = query.Where(hf => hf.HumanMajorName == HunmaMajorName);
			}
			// 添加对RegistTime的时间范围过滤条件（如果startDate和endDate都被设置了） 
			if (startDate.HasValue && endDate.HasValue)
			{
				query = query.Where(e => e.RegistTime >= startDate.Value && e.RegistTime <= endDate.Value);
			}
			return query.ToList();
		}

		


		public HFFy GetHFFy(int currentPage, int pageSize, string HumanFileStatus)
		{
			// 使用 Where 子句来过滤 IAM 实体
			IQueryable<HF> filteredIAMs = myDb.HF;
			if (!string.IsNullOrEmpty(HumanFileStatus))
			{
				filteredIAMs = filteredIAMs.Where(iam => iam.HumanFileStatus == HumanFileStatus);
			}
			// 应用分页
			List<HF> eMRs = filteredIAMs.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

			// 计算总计数（考虑过滤条件）
			int totalCount = filteredIAMs.Count();

			// 创建并返回结果
			HFFy result = new HFFy() { HFs = eMRs, TotalCount = totalCount };
			return result;
		}

		public async Task<bool> HFAdd(HF hF)
		{
			myDb.HF.Add(hF);
			int zhi = await myDb.SaveChangesAsync();
			return zhi > 0 ? true : false;
		}


		public List<HF> SelectHf(string HumanFileStatus)
		{

			var zhi = myDb.HF.Where(e => e.HumanFileStatus == HumanFileStatus).ToList();
			return zhi;
		}


		//修改档案状态
		public async Task<bool> Updatehf(HF hF)
		{
			var zhi = await myDb.HF.FindAsync(hF.HufId);
			if(zhi == null)
			{
				return false;
			}
			zhi.Checker = hF.Checker; //档案复核人
			zhi.AttachmentName = hF.AttachmentName; //档案附件
			zhi.CheckTime = hF.CheckTime; //档案复核时间
			zhi.HumanFileStatus = hF.HumanFileStatus; //档案状态
			myDb.HF.Update(zhi);
			int xg = await myDb.SaveChangesAsync();
			return xg > 0 ? true : false;
		}

		
					//HumanTelephone: khbh.value.HumanTelephone,//电话
					//HumanMobilephone: khbh.value.HumanMobilephone,//手机号码
					//HumanBank: khbh.value.HumanBank,//开户行
					//HumanAccount: khbh.value.HumanAccount,//银行账号
					//HumanQQ: khbh.value.HumanQQ,//QQ号码
					//HumanEmail: khbh.value.HumanEmail,//电子邮件
					//HumanHobby: this.khbh.HumanHobby,//爱好
					//HumanSpeciality: this.khbh.HumanSpeciality,//特长
					//HumanSex: this.khbh.HumanSex,//性别
					//HumanReligion: this.khbh.HumanReligion,//宗教信仰
					//HumanParty: this.khbh.HumanParty,//政治面貌
					//HumanNationality: this.khbh.HumanNationality,//国籍
					//HumanRace: this.khbh.HumanRace, //民族
					//HumanBirthday: khbh.value.HumanBirthday,//出生日期
					//HumanBirthplac: khbh.value.HumanBirthplace,//出生地
					//HumanAge: khbh.value.HumanAge,//年龄
					//HumanEducatedDegree: this.khbh.HumanEducatedDegree,//学历
					//HumanEducatedYears: this.khbh.HumanEducatedYears,//教育年限
					//HumanEducatedMajor: this.khbh.HumanEducatedMajor,//学历专业
					//HumanSocietySecurityId: khbh.value.HumanSocietySecurityId,//社会保障号
					//HumanIdCard: khbh.value.HumanIdCard,//身份证号
					//Remark: khbh.value.Remark,//备注
					//HumanFamilyMembership: khbh.value.HumanFamilyMembership,//家庭关系
					//HumanPicture: chargeForm.imageUrl,//相片
					//Changer: khbh.value.Register, //变更人
					//ChangeTime: new Date(formattedCurrentTime.value).toISOString(), //变更时间
					//LastlyChangeTime:new Date(formattedCurrentTime.value).toISOString(),//最近更改时间

		public async Task<bool> Updatehfbiao(HF hF)
		{
			var zhi = await myDb.HF.FindAsync(hF.HufId);
			if (zhi == null)
			{
				return false; 
			}
			zhi.HumanFamilyMembership = hF.HumanFamilyMembership; //家庭关系
			zhi.HumanPicture = hF.HumanPicture; //相片
			zhi.Changer = hF.Changer;  //变更人
			zhi.ChangeTime = hF.ChangeTime; //变更时间
			zhi.LastlyChangeTime = hF.LastlyChangeTime; //最近更改时间
			zhi.HumanBank = hF.HumanBank; //开户行
			zhi.HumanAccount = hF.HumanAccount; //银行账号
			zhi.HumanQQ = hF.HumanQQ; //QQ号码
			zhi.HumanEmail = hF.HumanEmail; //电子邮件
			zhi.HumanHobby = hF.HumanHobby; //爱好
			zhi.HumanSpeciality = hF.HumanSpeciality; //特长
			zhi.HumanSex = hF.HumanSex; //性别
			zhi.HumanReligion = hF.HumanReligion; //宗教信仰
			zhi.HumanParty = hF.HumanParty; //政治面貌
			zhi.HumanNationality = hF.HumanNationality; //国籍
			zhi.HumanRace = hF.HumanRace; //民族
			zhi.HumanBirthday = hF.HumanBirthday; //出生日期
			zhi.HumanBirthplace = hF.HumanBirthplace; //出生地
			zhi.HumanAge = hF.HumanAge; //年龄
			zhi.HumanEducatedDegree = hF.HumanEducatedDegree; //学历
			zhi.HumanEducatedYears = hF.HumanEducatedYears; //教育年限
			zhi.HumanEducatedMajor = hF.HumanEducatedMajor; //学历专业
			zhi.HumanSocietySecurityId = hF.HumanSocietySecurityId; //社会保障号
			zhi.HumanIdCard = hF.HumanIdCard; //身份证号
			zhi.Remark = hF.Remark; //备注
			zhi.HumanTelephone = hF.HumanTelephone; //电话
			zhi.HumanMobilephone = hF.HumanMobilephone; //手机号码
			zhi.AttachmentName = hF.AttachmentName; //档案附件
			zhi.HumanName = hF.HumanName; //姓名
			zhi.HumanAddress = hF.HumanAddress; //地址
			zhi.HumanPostcode = hF.HumanPostcode; //邮编

			myDb.HF.Update(zhi);
			int xg = await myDb.SaveChangesAsync();
			return xg > 0 ? true : false;
		}

		public async Task<bool> UpdatehfHumanFileStatus(HF hF)
		{
			var zhi = await myDb.HF.FindAsync(hF.HufId);
			if (zhi == null)
			{
				return false; 
			}
			zhi.HumanFileStatus = hF.HumanFileStatus; //状态
			myDb.HF.Update(zhi);
			int xg = await myDb.SaveChangesAsync();
			return xg > 0 ? true : false;
		}
	}
}
